import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import Icons from 'unplugin-icons/vite'
import IconResolver from 'unplugin-icons/resolver'
import { cwd } from 'process'
// https://vitejs.dev/config/
export default ({ mode }) => {
  const env = loadEnv(mode, cwd())
  console.log(env)
  return defineConfig({
    plugins: [
      vue(),
      AutoImport({
        dts: './src/@types/auto-import.d.ts',
        include: [
          /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
          /\.vue$/,
          /\.vue\?vue/, // .vue
          /\.md$/, // .md
        ],
        imports: ['vue'],
        resolvers: [
          ElementPlusResolver(),
          IconResolver({
            prefix: 'Icon',
          }),
        ],
      }),
      Components({
        resolvers: [
          IconResolver({
            enabledCollections: ['ep'],
          }),
          ElementPlusResolver(),
        ],
        dts: './src/@types/components.d.ts',
      }),
      Icons({
        autoInstall: true,
      }),
    ],
    build: {
      outDir: './dist',
      emptyOutDir: true,
      minify: true,
      rollupOptions: {
        external: ['vue', 'axios', 'vue-router', 'pinia'],
        output: {
          globals: {
            vue: 'Vue',
            axios: 'axios',
            vueRouter: 'vue-router',
            pinia: 'pinia',
          },
        },
      },
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true,
          pure_funcs: ['console.log'],
        },
      },
    },
    server: {
      hmr: true,
      open: true,
      port: 9527,
      host: true,
      proxy: {
        [`${env.VITE_BASE_PREFIX + env.VITE_BASE_VERSION}`]: {
          target: `${env.VITE_BASE_URL}`,
          changeOrigin: true,
          rewrite(path) {
            return path
          },
        },
      },
    },
    resolve: {
      alias: {
        '@src': '/src',
      },
    },
  })
}
